package com.loongtech.bi.action.realTime;

import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.realTime.BossRealTimeManager;
import com.loongtech.bi.manager.realTime.ServerRealTimeManager;
import com.loongtech.bi.manager.realTime.ServerRealTimeManager.OnlineRealTimeData;
import com.loongtech.bi.manager.realTime.ServerRealTimeManager.ServerCapacityData;
import com.loongtech.bi.manager.realTime.ServerRealTimeManager.ServerRealTimeData;
import com.loongtech.bi.manager.realTime.ServerRealTimeManager.ServerRealTimeDetailData;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 实时数据-分服概况Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class ServerRealTimeAction {
	private Date lookDate = Time.currentDate();  // 前端传过来的查看时间
	private Map<String, String> serverItmes;  // 区服下拉列表
	private List<String> selectedServers21; // 选中的区服21
	private List<String> selectedServers22; // 选中的区服22
	private List<String> selectedServers23; // 选中的区服23
	private List<String> selectedServers3; // 选中的区服3

	private String eventTagId = "online"; // 分服实时数据标签 online newUser activeUser
	
	private List<OnlineRealTimeData> onlineRealTimeDatas; // 项目整体实时在线数据
	private ChartUIData onlineRealTimeChart; // 项目整体实时在线图表
	private List<ServerRealTimeDetailData> serverRealTimeDetailDatas;  // 分服详细数据查询结果
	private ServerRealTimeDetailData allSumServerRealTimeDetailData; // 分服详细数据汇总结果
	private List<ServerRealTimeData> serverRealTimeDatas; // 分服实时在线数据
	private List<ServerCapacityData> serverCapacityDatas; // 服务器容量数据
	private ChartUIData serverRealTimeChart; // 分服实时在线图表
	private ChartUIData serverCapacityChart; // 服务器容量图表

    private String lastMaxOnlineNumStr = "";  // 前端要访问的当前最高负载：xx渠道xxx服xxxx人
    private String serverNumStr = "";  // 共xx组服务器

    private String selectedItem = "all";  // all=全部平台;ioszb=ios正版;hf=混服;yyb=应用宝;yhlm=硬核联盟

    private List<BossRealTimeManager.BossRealTimeDetailData> allDetailDatas;  // 所有详细数据
    private List<BossRealTimeManager.BossRealTimeDetailData> detailDatas;  // 前端要显示的详细数据
    private ChartUIData chartData;  // 前端要显示的曲线图数据
	
	@Autowired
	ServerRealTimeManager serverRealTimeManager;
    @Autowired
    BossRealTimeManager bossRealTimeManager;
	
	@Autowired
	SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		List<EntityBiServer> list = BiPlatformManager.SERVER_LIST;
		selectedServers21 = new ArrayList<>();
		selectedServers22 = new ArrayList<>();
		selectedServers23 = new ArrayList<>();
		selectedServers3 = new ArrayList<>();
		serverItmes = new LinkedHashMap<>();
		for (int i = list.size() - 1; i >= 0; i--) {
			EntityBiServer t = list.get(i);
			
			serverItmes.put(t.getName(), t.getId());
			
			if (selectedServers21.size() < 3) {
				selectedServers21.add(t.getId());
			}
			if (selectedServers22.size() < 3) {
				selectedServers22.add(t.getId());
			}
			if (selectedServers23.size() < 3) {
				selectedServers23.add(t.getId());
			}
			if (selectedServers3.size() < 3) {
				selectedServers3.add(t.getId());
			}
		}
		
		search();
	}
	
	public void search() {
		sysLogManager.addLog(session.getUserName(), "查询实时数据-分服概况");
		
		onlineRealTimeDatas = new ArrayList<>(); // 项目整体实时在线数据
		onlineRealTimeChart = null; // 项目整体实时在线图表
		serverRealTimeDetailDatas = new ArrayList<>();  // 分服详细数据查询结果
		allSumServerRealTimeDetailData = serverRealTimeManager.new ServerRealTimeDetailData(); // 分服详细数据汇总结果
		serverRealTimeDatas = new ArrayList<>(); // 分服实时在线数据
		serverCapacityDatas = null;
		serverRealTimeChart = null; // 分服实时在线图表
		serverCapacityChart = null; // 服务器容量图表

        //serverRealTimeManager.getSession().clear();

		// 分服详细数据
		serverRealTimeManager.updateServerRealTimeDetailData(lookDate, serverRealTimeDetailDatas, allSumServerRealTimeDetailData);
		
		// 整体实时在线
		/*onlineRealTimeDatas = serverRealTimeManager.getOnlineRealTimeDatas(lookDate);
		if (onlineRealTimeDatas != null && onlineRealTimeDatas.size() > 0) {
			onlineRealTimeChart = new ChartUIData();
			onlineRealTimeChart.setJsonStr(new ChartTool(onlineRealTimeDatas, OnlineRealTimeData.class, onlineRealTimeChart.getSelectItem()).loadData(onlineRealTimeChart.getJsonType()));
		} else {
			onlineRealTimeChart = null;
		}*/
		
		// 分服实时数据
		serverRealTimeDatas = serverRealTimeManager.getServerRealTimeDatas(lookDate, "online".equals(eventTagId) ? selectedServers21 : ("newUser".equals(eventTagId) ? selectedServers22 : selectedServers23));
		if (serverRealTimeDatas != null && serverRealTimeDatas.size() > 0) {
			serverRealTimeChart = new ChartUIData();
			serverRealTimeChart.setSelectItem(eventTagId);
            serverRealTimeChart.setJsonStr(new ChartTool(serverRealTimeDatas, ServerRealTimeData.class, serverRealTimeChart.getSelectItem()).loadData(serverRealTimeChart.getJsonType()));
        } else {
			serverRealTimeChart = null;
		}
		
		// 服务器容量数据
		/*serverCapacityDatas = serverRealTimeManager.getServerCapacityDatas(lookDate, selectedServers3);
		if (serverCapacityDatas != null && serverCapacityDatas.size() > 0) {
			serverCapacityChart = new ChartUIData();
			serverCapacityChart.setJsonStr(new ChartTool(serverCapacityDatas, ServerCapacityData.class, serverCapacityChart.getSelectItem()).loadData(serverCapacityChart.getJsonType()));
		} else {
			serverCapacityChart = null;
		}*/

        allDetailDatas = bossRealTimeManager.getBossRealTimeDetailData(lookDate);  // 详细数据
        selectItem();
	}

	public boolean renderOnlineData() {
	    return Time.getInterval(lookDate, Time.currentDate()) == 0;
    }

    public void selectItem() {
        if (allDetailDatas == null || allDetailDatas.size() <= 0) {
            detailDatas = null;
            chartData = null;
            lastMaxOnlineNumStr = "当前全服最高负载为0";
            serverNumStr = BossRealTimeManager.INDEX2NAME.get(selectedItem) + "共0组服务器";  // 服务器数量
            return;
        }

        Collections.sort(allDetailDatas, bossRealTimeManager.new BossRealTimeDetailData().comparatorOnlineReverse);
        BossRealTimeManager.BossRealTimeDetailData maxData = allDetailDatas.get(allDetailDatas.size() - 1);
        lastMaxOnlineNumStr = "当前全服最高负载：" + maxData.getItemName() + "渠道" + maxData.getServername() + "服" + maxData.getOnlinenum() + "人";

        if ("all".equals(selectedItem)) {
            detailDatas = allDetailDatas;
        } else {
            detailDatas = new ArrayList<>();
            for (BossRealTimeManager.BossRealTimeDetailData d : allDetailDatas) {
                if (selectedItem.equals(d.getItem())) {
                    detailDatas.add(d);
                }
            }
        }

        // 过滤未开放的服
        Iterator<BossRealTimeManager.BossRealTimeDetailData> iter = detailDatas.iterator();
        while(iter.hasNext()){
            EntityBiServer s = BiPlatformManager.SERVER_MAP.get(iter.next().getServerid());
            if (s == null) {
                iter.remove();
            }
        }

        if (detailDatas != null && detailDatas.size() > 0) {
            serverNumStr = BossRealTimeManager.INDEX2NAME.get(selectedItem) + "共" + detailDatas.size() + "组服务器";  // 服务器数量

            chartData = new ChartUIData();
            chartData.setJsonType(ChartType.COLUMN.type);
            chartData.setJsonStr(new ChartTool(detailDatas.size() > 20 ? detailDatas.subList(detailDatas.size() - 20, detailDatas.size()) : detailDatas, BossRealTimeManager.BossRealTimeDetailData.class, chartData.getSelectItem()).loadData(chartData.getJsonType()));

            Collections.sort(detailDatas, bossRealTimeManager.new BossRealTimeDetailData().comparatorOnline);
        } else {
            serverNumStr = BossRealTimeManager.INDEX2NAME.get(selectedItem) + "共0组服务器";  // 服务器数量
            chartData = null;
        }
    }
	
	// 分服实时数据选择服
	public void handleServerChange2() {
		serverRealTimeDatas = serverRealTimeManager.getServerRealTimeDatas(lookDate, "online".equals(eventTagId) ? selectedServers21 : ("newUser".equals(eventTagId) ? selectedServers22 : selectedServers23));

		if (serverRealTimeDatas != null && serverRealTimeDatas.size() > 0) {
			serverRealTimeChart = new ChartUIData();
			serverRealTimeChart.setSelectItem(eventTagId);
			serverRealTimeChart.setJsonStr(new ChartTool(serverRealTimeDatas, ServerRealTimeData.class, serverRealTimeChart.getSelectItem()).loadData(serverRealTimeChart.getJsonType()));
        } else {
			serverRealTimeChart = null;
		}
	}
	
	// 服务器容量数据选择服
	public void handleServerChange3() {
		serverCapacityDatas = serverRealTimeManager.getServerCapacityDatas(lookDate, selectedServers3);
		if (serverCapacityDatas != null && serverCapacityDatas.size() > 0) {
			serverCapacityChart = new ChartUIData();
			serverCapacityChart.setJsonStr(new ChartTool(serverCapacityDatas, ServerCapacityData.class, serverCapacityChart.getSelectItem()).loadData(serverCapacityChart.getJsonType()));
		} else {
			serverCapacityChart = null;
		}
	}
	
	// 分服实时数据切换标签页
	public void onCompareTabChange(TabChangeEvent event) {
		eventTagId = event.getTab().getId();
		
		if (serverRealTimeDatas != null && serverRealTimeDatas.size() > 0) {
			serverRealTimeChart = new ChartUIData();
			serverRealTimeChart.setSelectItem(eventTagId);
			serverRealTimeChart.setJsonStr(new ChartTool(serverRealTimeDatas, ServerRealTimeData.class, serverRealTimeChart.getSelectItem()).loadData(serverRealTimeChart.getJsonType()));
		} else {
			serverRealTimeChart = null;
		}
	}
	
	public Date getLookDate() {
		return lookDate;
	}

	public void setLookDate(Date lookDate) {
		this.lookDate = lookDate;
	}
	
	public List<ServerRealTimeDetailData> getServerRealTimeDetailDatas() {
		return serverRealTimeDetailDatas;
	}

	public void setServerRealTimeDetailDatas(
			List<ServerRealTimeDetailData> serverRealTimeDetailDatas) {
		this.serverRealTimeDetailDatas = serverRealTimeDetailDatas;
	}

	public ServerRealTimeDetailData getAllSumServerRealTimeDetailData() {
		return allSumServerRealTimeDetailData;
	}

	public void setAllSumServerRealTimeDetailData(
			ServerRealTimeDetailData allSumServerRealTimeDetailData) {
		this.allSumServerRealTimeDetailData = allSumServerRealTimeDetailData;
	}

	public Map<String, String> getServerItmes() {
		return serverItmes;
	}

	public void setServerItmes(Map<String, String> serverItmes) {
		this.serverItmes = serverItmes;
	}

	public ChartUIData getServerCapacityChart() {
		return serverCapacityChart;
	}

	public void setServerCapacityChart(ChartUIData serverCapacityChart) {
		this.serverCapacityChart = serverCapacityChart;
	}
	
	public ChartUIData getServerRealTimeChart() {
		return serverRealTimeChart;
	}

	public void setServerRealTimeChart(ChartUIData serverRealTimeChart) {
		this.serverRealTimeChart = serverRealTimeChart;
	}

	public List<ServerRealTimeData> getServerRealTimeDatas() {
		return serverRealTimeDatas;
	}

	public void setServerRealTimeDatas(List<ServerRealTimeData> serverRealTimeDatas) {
		this.serverRealTimeDatas = serverRealTimeDatas;
	}

	public String getEventTagId() {
		return eventTagId;
	}

	public void setEventTagId(String eventTagId) {
		this.eventTagId = eventTagId;
	}

	public List<OnlineRealTimeData> getOnlineRealTimeDatas() {
		return onlineRealTimeDatas;
	}

	public void setOnlineRealTimeDatas(List<OnlineRealTimeData> onlineRealTimeDatas) {
		this.onlineRealTimeDatas = onlineRealTimeDatas;
	}

	public ChartUIData getOnlineRealTimeChart() {
		return onlineRealTimeChart;
	}

	public void setOnlineRealTimeChart(ChartUIData onlineRealTimeChart) {
		this.onlineRealTimeChart = onlineRealTimeChart;
	}

	public List<String> getSelectedServers21() {
		return selectedServers21;
	}

	public void setSelectedServers21(List<String> selectedServers21) {
		this.selectedServers21 = selectedServers21;
	}

	public List<String> getSelectedServers22() {
		return selectedServers22;
	}

	public void setSelectedServers22(List<String> selectedServers22) {
		this.selectedServers22 = selectedServers22;
	}

	public List<String> getSelectedServers23() {
		return selectedServers23;
	}

	public void setSelectedServers23(List<String> selectedServers23) {
		this.selectedServers23 = selectedServers23;
	}

	public List<String> getSelectedServers3() {
		return selectedServers3;
	}

	public void setSelectedServers3(List<String> selectedServers3) {
		this.selectedServers3 = selectedServers3;
	}

	public List<ServerCapacityData> getServerCapacityDatas() {
		return serverCapacityDatas;
	}

	public void setServerCapacityDatas(List<ServerCapacityData> serverCapacityDatas) {
		this.serverCapacityDatas = serverCapacityDatas;
	}

    public String getLastMaxOnlineNumStr() {
        return lastMaxOnlineNumStr;
    }

    public void setLastMaxOnlineNumStr(String lastMaxOnlineNumStr) {
        this.lastMaxOnlineNumStr = lastMaxOnlineNumStr;
    }

    public String getServerNumStr() {
        return serverNumStr;
    }

    public void setServerNumStr(String serverNumStr) {
        this.serverNumStr = serverNumStr;
    }

    public String getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(String selectedItem) {
        this.selectedItem = selectedItem;
    }

    public List<BossRealTimeManager.BossRealTimeDetailData> getAllDetailDatas() {
        return allDetailDatas;
    }

    public void setAllDetailDatas(List<BossRealTimeManager.BossRealTimeDetailData> allDetailDatas) {
        this.allDetailDatas = allDetailDatas;
    }

    public List<BossRealTimeManager.BossRealTimeDetailData> getDetailDatas() {
        return detailDatas;
    }

    public void setDetailDatas(List<BossRealTimeManager.BossRealTimeDetailData> detailDatas) {
        this.detailDatas = detailDatas;
    }

    public ChartUIData getChartData() {
        return chartData;
    }

    public void setChartData(ChartUIData chartData) {
        this.chartData = chartData;
    }
}
